package org.example.studyonline.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studyonline.mapper.ChapterMapper;
import org.example.studyonline.mapper.CourseCategoryMapper;
import org.example.studyonline.mapper.CourseMapper;
import org.example.studyonline.pojo.Chapter;
import org.example.studyonline.pojo.Course;
import org.example.studyonline.pojo.DTO.AddCourseDTO;
import org.example.studyonline.pojo.DTO.DeleteCourseDTO;
import org.example.studyonline.pojo.DTO.UpdateCourseDTO;
import org.example.studyonline.pojo.VO.ChapterVO;
import org.example.studyonline.pojo.VO.CourseListVO;
import org.example.studyonline.pojo.PageBean;
import org.example.studyonline.pojo.VO.CourseVO;
import org.example.studyonline.pojo.VO.StatisticsVO;
import org.example.studyonline.service.CourseService;
import org.example.studyonline.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;

@Service
public class CourseListImpl extends ServiceImpl<CourseMapper,Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Resource
    private ChapterMapper chapterMapper;

    @Resource
    private CourseCategoryMapper courseCategoryMapper;
    @Override
    public PageBean<CourseListVO> getCourseList(Long pageNum, Long pageSize) {

        IPage<CourseListVO> page = new Page<>(pageNum, pageSize);

        courseMapper.getCourseList(page,null);

        PageBean<CourseListVO> result = new PageBean<>();
        result.setPage(pageNum);
        result.setTotal(page.getPages());
        result.setItems(page.getRecords());

        return result;

    }

    @Override
    public CourseVO getChapters(Integer courseID) {
        // 先查课程信息
        Course course = courseMapper.selectById(courseID);
        if (ObjectUtils.isEmpty(course)){
            throw new RuntimeException("课程不存在");
        }
        CourseVO courseVO = BeanCopyUtils.copyBean(course, CourseVO.class);
        LambdaQueryWrapper<Chapter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Chapter::getCourseID,courseID);
        List<Chapter> chapters = chapterMapper.selectList(queryWrapper);
        List<ChapterVO> chapterVOS = BeanCopyUtils.copyBeanList(chapters, ChapterVO.class);
        courseVO.setChapters(chapterVOS);
        return courseVO;
    }

    @Override
    public void update(UpdateCourseDTO updateCourseDTO) {
        LambdaUpdateWrapper<Course> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Course::getCourseID,updateCourseDTO.getCourseID());
        updateWrapper.set(Course::getName,updateCourseDTO.getName());
        updateWrapper.set(Optional.ofNullable(updateCourseDTO.getDescription()).filter(des->!des.isEmpty()).isPresent(),Course::getDescription,updateCourseDTO.getDescription());
        courseMapper.update(updateWrapper);
    }

    @Override
    public void add(AddCourseDTO addCourseDTO) {
        Course course = BeanCopyUtils.copyBean(addCourseDTO, Course.class);
        courseMapper.insert(course);
    }

    @Override
    public void delete(DeleteCourseDTO deleteCourseDTO) {
        courseMapper.deleteById(deleteCourseDTO.getCourseID());
    }

    @Override
    public PageBean<StatisticsVO> getStatistics(Long pageNo,Long pageSize) {
        Long offset = (pageNo - 1) * pageSize;
        List<StatisticsVO> list = courseCategoryMapper.getCourseListByPage(offset,pageSize);
        long total = courseCategoryMapper.getCourseListTotal();
        PageBean<StatisticsVO> pageBean = new PageBean<>();
        pageBean.setPage(pageNo);
        pageBean.setTotal(total);
        pageBean.setItems(list);
        return pageBean;
    }

    @Override
    public PageBean<CourseListVO> searchCourse(String keyword, Long pageNo, Long pageSize) {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Course::getName,keyword);
        Page<Course> page = new Page<>(pageNo, pageSize);
        page(page, queryWrapper);
        List<Course> records = page.getRecords();
        List<CourseListVO> courseListVOS = BeanCopyUtils.copyBeanList(records, CourseListVO.class);
        PageBean<CourseListVO> pageBean = new PageBean<>();
        pageBean.setPage(pageNo);
        pageBean.setTotal(page.getTotal());
        pageBean.setItems(courseListVOS);
        return pageBean;
    }

}
